package com.company.cloud.article.modules.app.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.company.cloud.article.modules.app.controller.resp.ArticleModelWithBehave;
import com.company.cloud.article.modules.cache.models.ArticleCategoryModel;
import com.company.cloud.article.modules.cache.utils.ArticleConvertUtils;
import com.company.cloud.behavior.modules.api.response.BehaviorSearchAllModel;
import com.company.cloud.behavior.modules.api.response.BehaviorSearchAllParam;
import com.company.cloud.behavior.modules.api.response.BehaviorSearchListAllParam;
import com.company.cloud.behavior.modules.api.service.BehaviorClientService;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.constant.BaseConstant;
import com.company.common.server.constant.IRelBusiType;
import com.company.common.server.log.LogSaasUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.company.cloud.article.modules.cache.models.ArticleModel;
import com.company.cloud.article.modules.app.controller.req.ArticleQuery4PageParam;
import com.company.cloud.article.modules.app.service.ArticleAppService;
import com.company.cloud.article.modules.cache.service.ArticleCacheService;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.redis.service.RedisUtils;
import com.company.cloud.common.utils.AuthContextUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

@RestController
@RequestMapping("/app/cms/article")
@Api(tags = "App_cms_公司新闻资讯")
public class ArticleController {

    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private ArticleAppService articleAppService;
    @Autowired
    private ArticleCacheService articleCacheService;
    @Autowired
    private BehaviorClientService behaviorClientService;

    @PostMapping("/query")
    @ApiOperation(value = "查询列表", response = ArticleModelWithBehave.class)
    public BaseResult query(@RequestBody ArticleQuery4PageParam params) {
        params.setState(BaseConstant.YNEnum.YES.getCode());
        IPage<ArticleModel> pageList = articleAppService.queryArticle(params, AuthContextUtils.getCompanyNo());
        BehaviorSearchListAllParam param = new BehaviorSearchListAllParam();
        LoginSimpleUserInfo loginSimpleUserInfo = AuthContextUtils.getUserNoValidate();
        if (loginSimpleUserInfo != null) {
            param.setUserId(loginSimpleUserInfo.getId());
        }
        param.setObjType(IRelBusiType.CMS_Article);
        param.setCollectionFlg(true);
        param.setCommentFlg(true);
        param.setThumbsUpFlg(true);
        List<String> objNo = new ArrayList<>();
        for (ArticleModel model: pageList.getRecords()) {
            objNo.add(model.getArticleRelNo());
        }
        param.setObjNo(objNo);
        Map<String, BehaviorSearchAllModel> behaviorMap = behaviorClientService.getBehaviorMap(param).getData();
        IPage<ArticleModelWithBehave> data=pageList.convert(new Function<ArticleModel, ArticleModelWithBehave>() {
            @Override
            public ArticleModelWithBehave apply(ArticleModel model) {
                return ArticleConvertUtils.getArticleModelWithBehave(model, behaviorMap);
            }
        });
        return BaseResult.ok(data);
    }


    @PostMapping("/queryRecommend")
    @ApiOperation(value = "推荐列表", response = ArticleModel.class)
    public BaseResult queryRecommend(@RequestBody ArticleQuery4PageParam params) {
        params.setState(BaseConstant.YNEnum.YES.getCode());
        params.setPage(1);
        params.setRows(100);
        IPage<ArticleModel> pageList = articleAppService.queryArticle(params, AuthContextUtils.getCompanyNo());

        if(pageList==null || CollUtil.isEmpty(pageList.getRecords())){
            return BaseResult.ok();
        }

        if(pageList.getRecords().size()<=10){
            List<ArticleModel> data=new ArrayList<>();
            pageList.getRecords().forEach(item->{
                data.add(item);
            });
            return BaseResult.ok(data);
        }else{
            List<ArticleModel> data= RandomUtil.randomEles(pageList.getRecords(),10);
            return BaseResult.ok(data);
        }

    }

    @GetMapping("/get")
    @ApiOperation(value = "获取详情", response = ArticleModelWithBehave.class)
    public BaseResult get(@RequestParam("companyArticleNo") String companyArticleNo) {
        //todo:转拦截器和MQ
//        LogSaasUtils.saveUserBrowseObjLogOne(ubaLogRemoteService, IRelBusiType.CMS_Article,companyArticleNo);
        ArticleModel model = articleCacheService.getCompanyArticleByNoCache(companyArticleNo);
        LoginSimpleUserInfo loginSimpleUserInfo = AuthContextUtils.getUserNoValidate();
        BehaviorSearchAllParam param = new BehaviorSearchAllParam();
        if (loginSimpleUserInfo != null) {
            param.setUserId(loginSimpleUserInfo.getId());
        }
        param.setObjType(IRelBusiType.CMS_Article);
        param.setObjNo(model.getArticleRelNo());
        param.setCollectionFlg(true);
        param.setCommentFlg(true);
        param.setThumbsUpFlg(true);
        ArticleModelWithBehave result = ArticleConvertUtils.getArticleModelWithBehave(model, behaviorClientService.getBehaviorInfo(param).getData());
        return BaseResult.ok(result);
    }

    @GetMapping("/getNew")
    @ApiOperation(value = "获取详情", response = ArticleModelWithBehave.class)
    public BaseResult getNew(@RequestParam("companyArticleNo") String companyArticleNo) {
        return null;
    }

    @GetMapping("/getTree")
    @ApiOperation(value = "分类树", response = ArticleCategoryModel.class)
    public BaseResult getTree(@RequestParam("companyNo") String companyNo) {
        return BaseResult.ok(articleCacheService.getArticleCategoryByCompanyDB(companyNo));
    }

}
